package com.javenwong.android.engine.scene;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;

import com.javenwong.android.engine.GameActivity;
import com.javenwong.android.engine.GameCanvas;
import com.javenwong.android.engine.Graphics;
import com.javenwong.android.engine.ImageManager;
import com.javenwong.android.engine.View;
import com.javenwong.android.engine.biology.Actor;

/**
 * 场景
 * 
 * @author 王晓锋
 * @version 1.00
 */
public class Scene extends View {

	/**
	 * 场景id -> 对应 {id}.sce 文件
	 */
	protected String id;

	/**
	 * 地图id -> 对应 {id}.m 文件
	 */
	protected byte mapId;

	/** 摄像机 */
	public Camera camera;

	/**
	 * 地图
	 */
	protected Map map;

	/**
	 * 地图横向循环次数
	 */
	protected byte hfor;
	/**
	 * 地图纵向循环次数
	 */
	protected byte vfor;

	/**
	 * Actor集合
	 */
	protected Vector actors = new Vector();

	/** 场景宽度 */
	protected int width;
	/** 场景 高度 */
	protected int height;

	/** 是否填充背景 */
	protected boolean fillBackground = true;

	/**
	 * 构造方法
	 * 
	 * @param id
	 *            场景id
	 */
	public Scene(String id) {
		this.id = id; 
		// System.out.println("sence: "+this.toString());  
		loadScene(id);
	}

	public Scene() { 
	}

	/**
	 * 加载场景
	 * 
	 * @param id
	 */
	protected void loadScene(String id) {
		InputStream in = null;
		DataInputStream dis = null;
		try {
			in = GameActivity.assetManager.open("scene/" + id + ".sce");

			dis = new DataInputStream(in);

			// 创建摄像机
			camera = new Camera(this);

			// 地图id
			mapId = dis.readByte();
			// 横向地图循环次数
			hfor = dis.readByte();
			// 纵向地图循环次数
			vfor = dis.readByte();

			// 创建地图
			map = new Map(String.valueOf(mapId), camera);
			map.processHVFor(hfor, vfor);
			// map.processHVFor((byte)2, (byte)2);

			// map没有扩充数据
			// width = map.getWidth()*hfor;
			// height = map.getHeight()*vfor;

			// map已经扩充数据
			width = map.getWidth();
			height = map.getHeight();

			// Actor总数
			byte actorCount = dis.readByte();

			System.out.println("actorCount: " + actorCount);

			for (byte i = 0; i < actorCount; i++) {
				// Actor id -> i

				// Actor的 坐标 （该坐标是真实坐标 无须再运算）
				short x = dis.readShort();
				short y = dis.readShort();

				String name = dis.readUTF(); // Actor名称

				// Actor的类型 0-怪物 1-Npc 2-地图动画 3-Boss 4-触发区域 5-...
				// byte type = dis.readByte();

				// 动画组
				String group = dis.readUTF();

				System.out.println("group: " + group);

				// 默认动画
				String ani = dis.readUTF();

				// System.out.println("default ani: "+ani);

				// Actor属性总数
				byte paramCount = dis.readByte();

				Vector<String> params = new Vector<String>();

				for (byte j = 0; j < paramCount; j++) {
					// 属性
					// char param = (char)in.read();
					// 属性值
					// short value = dis.readShort();
					String value = dis.readUTF();

					// params.put(new Character(param), new Short(value));
					params.add(value);
				}

				// 创建Actor
				Actor actor = new Actor(i, x, y, name, group, params);
				actor.setScene(this);
				actor.setDefaultAnimation(ani);

				// 添加Actor到场景
				actors.addElement(actor);
			}

			// System.out.println("actors.size(): "+actors.size());

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
			}
		}
	}

	protected void drawMap(Canvas g) {
		// map.draw(g, 0, (byte)2, (byte)1);
		// map.draw(g, 1, (byte)2, (byte)1);
		// map.draw(g, 2, (byte)2, (byte)1);

		map.draw(g);
	}

	protected void drawActors(Canvas g) {
		for (int i = 0; i < actors.size(); i++) {
			Actor actor = (Actor) actors.elementAt(i);

			actor.draw(g, camera);
		}
	}

	public Camera getCamera() {
		return camera;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public Map getMap() {
		return map;
	}

	public Vector getActors() {
		return actors;
	}

	public void setActors(Vector actors) {
		this.actors = actors;
	}

	public void setCamera(Camera camera) {
		this.camera = camera;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public boolean isFillBackground() {
		return fillBackground;
	}

	public void setFillBackground(boolean fillBackground) {
		this.fillBackground = fillBackground;
	}

	@Override
	public void logic() {

		// Actor逻辑
		for (int i = 0; i < actors.size(); i++) {
			((Actor) actors.elementAt(i)).logic();
		}

	}

	@Override
	public void draw(Canvas g) {

		// 填充画布
		// if (fillBackground){
		// Graphics.PAINT.setColor(Color.GRAY);
		// Graphics.fillRect(g, 0, 0, ImageManager.STANDARD_WIDTH,
		// ImageManager.STANDARD_HEIGHT);
		// }

		// System.out.println(getWidth()+" , "+getHeight());

		// 画地图
		drawMap(g);

		// 画Actors
		drawActors(g);

		// 画场景边框
		// g.setColor(0xFF0000);
		// g.drawRect(-getCamera().getX(), -getCamera().getY(), width-1,
		// height-1);
	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTouchEvent(MotionEvent event, float xF, float yF, int x, int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTouchDownEvent(MotionEvent event, float xF, float yF, int x, int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTouchUpEvent(MotionEvent event, float xF, float yF, int x, int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTouchMoveEvent(MotionEvent event, float xF, float yF, int x, int y) {
		// TODO Auto-generated method stub

	}

}
